<template>
  <div class="my-release">
    <div class="release-top">
     <div class="release-top-desc">
      <p>选择数据采集源<RefreshRight /></p>
      <p>选择单个网站点击开始爬虫获取数据</p>
     </div>
     <div class="release-top-right"></div>
    </div>
    <div class="release-content">
      <ul class="step-list">
        <li :class="{active: release.state === 1}">
          <span class="step-list-no">1</span>
          <span class="step-list-tit">选择数据采集源</span>
          <i class="line"></i>
        </li>
        <li :class="{active: release.state === 2}">
          <span class="step-list-no">
            <i class="el-icon-loading" v-if="release.state === 2"></i>
            <template v-else>2</template>
          </span>
          <span class="step-list-tit">智能爬取数据</span>
          <i class="line"></i>
        </li>
        <li :class="{active: release.state === 3}">
          <span class="step-list-no">3</span>
          <span class="step-list-tit">生成可视化报告</span>
        </li>
      </ul>

      <div class="choose">
        <span class="choose-tit">选择网站</span>
        <el-select class="choose-select" v-model="webName" placeholder="请选择">
          <el-option
            v-for="item in release.sites"
            :disabled="release.state !== 1"
            :key="item.Id"
            :label="item.SiteName"
            :value="item.SiteName">
          </el-option>
        </el-select>
      </div>
      <div class="search">
        <el-button type="primary" v-if="release.state === 1" @click="startTask">开始爬虫</el-button>
        <el-button type="primary" v-if="release.state === 2" @click="goDataView">数据可视化</el-button>
        <el-button type="primary" v-if="release.state === 3" @click="crawlTaskConfirmDone()">重新爬虫</el-button>
        <el-button type="primary" v-if="release.state === 3" @click="createBg()">生成报告</el-button>
      </div>
      <div class="deacinfo">
        <p class="deacinfo-tit">说明</p>
        <p>选择单个网站点击开始爬虫获取数据</p>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, reactive } from 'vue';
import { ElMessage } from "element-plus";
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'MyRelease',
  setup() {
    const store = useStore();
    const router = useRouter();
    const webName = ref('');
    const release = reactive({
      state: 1,
      sites: [],
    });
    // 获取我的发布列表
    const getSiteList = async () => {
      const data = await store.dispatch('task/querySiteList');
      release.sites = data;
      const istwo = data.filter(item => item.CrawlStatus === '2');
      const isThree = data.filter(item => item.CrawlStatus === '3');
      if (istwo.length > 0) {
        release.state = 2;
        webName.value = istwo[0].SiteName;
      }
      if (isThree.length > 0) {
        release.state = 3;
        webName.value = isThree[0].SiteName;
      }
      
      // const result = await store.dispatch('task/crawlTaskConfirmDone');
      // console.log(result);
    };
    const crawlTaskConfirmDone = async () => {
      release.state = 1
      await store.dispatch('task/crawlTaskConfirmDone', {
        site_name: webName.value
      });
    };
    const goDataView = () => {
      router.push({ name: 'dataVisual' });
    };
    const createBg = () => {
      router.push({ name: 'dashboard' });
    };
    // 开启爬虫任务
    const runTask = async () => {
      const data = await store.dispatch('task/startTask', {
        site_name: webName.value
      });
      release.state = 2;
      console.log(data);
      // release.sites = data;
    };
    const startTask = async () => {
      if (!webName.value) {
        ElMessage.warning("请先选择要爬取的网站");
        return;
      }
      runTask();
    };
    onMounted(() => {
      getSiteList();
    });
    return {
      release,
      webName,
      createBg,
      startTask,
      goDataView,
      crawlTaskConfirmDone,
    };
  },
});
</script>
<style lang="scss" scoped>
.my-release {
  .release-top {
    @extend %container;
    &-desc {
      display: flex;
      flex-direction: column;
      p {
        &:nth-child(1) {
          color: #282d30;
          font-size: 18px;
          font-weight: bold;
        }
        &:nth-child(2) {
          font-size: 12px;
          color: #848899;
          margin-top: 10px;
        }
      }
    }
  }
  .release-content {
    @extend %container;
    margin-top: 10px;
    .step-list {
      padding: 40px 100px;
      display: flex;
      justify-content: space-between;
      li {
        display: flex;
        cursor: pointer;
        align-items: center;
        &:nth-child(1) {
          width: calc((100% - 160px) / 2 );
        }
        &:nth-child(2) {
          width: calc((100% - 160px) / 2 );
        }
        &:nth-child(3) {
          width: 160px;
        }
      }
      .active {
        .step-list-no {
          background: #0054fe;
          border: #0054fe solid 1px;
          color: #ffffff;
        }
        .step-list-tit {
          color: #282d30;
        }
      }
      &-no {
        width: 30px;
        height: 30px;
        display: block;
        color: #d9d9d9;
        border: #d9d9d9 solid 1px;
        border-radius: 50%;
        line-height: 30px;
        text-align: center;
        font-size: 16px;
      }
      &-tit {
        font-size: 16px;
        color: #d8d8d8;
        line-height: 30px;
        margin-left: 10px;
      }
      .line {
        display: block;
        flex: 1;
        align-items: center;
        margin: 0 40px;
        height: 1px;
        background: #d9d9d9;
      }
    }
    .choose {
      display: flex;
      align-content: center;
      justify-content: center;
      margin: 100px 0 100px;
      &-tit {
        color: #787b7d;
        line-height: 40px;
        margin-right: 20px;
      }
      &-select {
        width: 60%;
      }
    }
    .search {
      display: flex;
      justify-content: center;
      &::v-deep(.el-button) {
        background-color: #0054fe;
        border-color: #0054fe;
      }
    }
    .deacinfo {
      background: #f8f9fb;
      padding: 20px;
      margin-top: 100px;
      p {
        font-size: 12px;
        color: #a5a9b5;
        line-height: 20px;
      }
      &-tit {
        font-size: 16px!important;
        color: #5c6063!important;
        margin-bottom: 10px;
      }
    }
  }
  .el-form-item {
    margin-bottom: 10px;
  }
  .submit-btn {
    text-align: right;
  }
  .table-expand {
    .el-form-item {
      margin-bottom: 0;
      .el-rate {
        margin: 10px 0;
      }
    }
  }
}
</style>